package ucd.csi.comp30220.fall2011.oteop.navigation;

import java.io.Serializable;
import java.text.DecimalFormat;

/**
 * Latitude and Longitude as decimals with the ability to show a string in
 * degrees, minutes, seconds notation.
 * 
 * @author David Harrison
 */
public class Position implements Serializable {

    private static final long serialVersionUID = 3741862046752042065L;

    private static final DecimalFormat FOURPLACES = new DecimalFormat("##.####");
    
    private double latitude;
    private double longitude;

    public Position(double latitude, double longitude) {
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    private static String latitudeToString(double degrees) {
        return degreesToString(degrees, "N", "S");
    }

    private static String longitudeToString(double degrees) {
        return degreesToString(degrees, "E", "W");
    }

    private static String degreesToString(double degrees, String pos, String neg) {
        double t = java.lang.Math.abs(degrees);
        int d = (int) t;
        t = (t - d) * 60.0;
        int m = (int) t;
        double s = (t - m) * 60.0;
        return d + "d" + m + "'" + FOURPLACES.format(s) + "\"" + (degrees < 0 ? neg : pos);
    }

    public String toString() {
        return latitudeToString(latitude) + " " + longitudeToString(longitude);
    }
}
